package com.chinacache.maint.workorder.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.chinacache.maint.commons.entity.BaseEntity;
import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.util.DateUtils;
import com.chinacache.maint.commons.util.HttpUtil;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.workorder.entity.Field;
import com.chinacache.maint.workorder.entity.FieldAttribute;
import com.chinacache.maint.workorder.entity.FieldClass;
import com.chinacache.maint.workorder.entity.FieldClassToField;
import com.chinacache.maint.workorder.entity.Flow;
import com.chinacache.maint.workorder.entity.FlowFieldAttribute;
import com.chinacache.maint.workorder.entity.FlowFieldClass;
import com.chinacache.maint.workorder.entity.WorkOrderStep;
import com.chinacache.maint.workorder.service.FieldAttributeManager;
import com.chinacache.maint.workorder.service.FlowFieldAttributeManager;
import com.chinacache.maint.workorder.service.FlowManager;
import com.chinacache.maint.workorder.service.WorkOrderManager;
import com.chinacache.maint.workorder.service.WorkOrderStepManager;

@SuppressWarnings("serial")
public class FlowFieldAttributeAction extends
		CRUDActionSupport<FlowFieldAttribute> {
	private FlowFieldAttributeManager flowFieldAttributeManager;
	private FieldAttributeManager fieldAttributeManager;
	private WorkOrderStepManager workOrderStepManager;
	private WorkOrderManager workOrderManager;
	private FlowManager flowManager;
	private FlowFieldAttribute entity;
	private Integer id;
	private Page<FlowFieldAttribute> page = new Page<FlowFieldAttribute>();
	private Map<String, Map> view = null;

	private Integer flowClassId;
	private Integer flowOrder;
	private Integer flowStatus;
	private Integer flowId;
	private Integer workOrderId;

	// private String condition;

	@Override
	public String delete() throws Exception {
		flowFieldAttributeManager.deleteFlowFieldAttribute(id);
		return RELOAD;
	}

	@Override
	public FlowFieldAttribute getModel() {
		return entity;
	}

	@Override
	public Page<FlowFieldAttribute> getPage() {
		return page;
	}

	public String next() throws Exception {
		Flow flow = flowManager.getFlow(flowId);
		List<FieldAttribute> allFa = new ArrayList<FieldAttribute>();
		for (FlowFieldClass ffc : flow.getFlowFieldClasss()) {
			FieldClass fieldClass = ffc.getFieldClass();
			for (FieldClassToField fctField : fieldClass.getFieldClassFields()) {
				Field field = fctField.getField();
				allFa.addAll(field.getFieldAttibutes());
			}
		}
		WorkOrderStep wos = new WorkOrderStep();
		wos.setFlow(flow);
		wos.setWorkOrder(workOrderManager.getWorkOrder(workOrderId));
		wos.setTime(DateUtils.getCurrDate());
		wos.setUsername((String) HttpUtil.getFromSession("username"));
		workOrderStepManager.saveWorkOrderStep(wos);
		for (FieldAttribute fa : allFa) {
			String value = HttpUtil.getHttpServletRequest().getParameter(
					fa.getGroupId().toString());
			if (value != null && value.length() > 0) {
				entity.setValue(value);
				entity.setFlow(flow);
				entity.setWorkOrderStep(wos);
				entity.setGroupId(fa.getGroupId());
			}
		}
		flowFieldAttributeManager.saveFlowFieldAttribute(entity);
		Flow nextFlow = flowManager.getFlowByOrderAndFlowClassId(flowOrder + 1,
				flowClassId);
		flowOrder = nextFlow.getOrder();
		flowStatus = nextFlow.getStatus();
		return flow();
	}

	@SuppressWarnings("unchecked")
	public String flow() throws Exception {
		Flow inputFlow = null;
		if (flowStatus != null && flowStatus == BaseEntity.FLOW_STATUS_END) {
			return list();
		} else {
			if (flowOrder == null) {
				inputFlow = flowManager.getFlowByStatusAndFlowClassId(
						BaseEntity.FLOW_STATUS_START, flowClassId);
			} else {
				inputFlow = flowManager.getFlowByOrderAndFlowClassId(flowOrder
						.intValue() - 1, flowClassId);
			}
			Map<String, List> fas = new HashMap<String, List>();
			for (FlowFieldClass ffc : inputFlow.getFlowFieldClasss()) {
				FieldClass fieldClass = ffc.getFieldClass();
				List<List> fasList = new ArrayList<List>();
				for (FieldClassToField fctField : fieldClass
						.getFieldClassFields()) {
					view = new HashMap<String, Map>();
					Field field = fctField.getField();
					Set<FieldAttribute> faSet = field.getFieldAttibutes();
					List<Integer> groupIds = new ArrayList<Integer>();
					for (FieldAttribute fa : faSet) {
						if (!groupIds.contains(fa.getGroupId())) {
							groupIds.add(fa.getGroupId());
						}
					}
					for (Integer groupId : groupIds) {
						List faByGroupIdList = fieldAttributeManager
								.getFieldAttributeByGroupId(groupId);
						fasList.add(faByGroupIdList);

					}
					fas.put(field.getName(), fasList);

				}
				view.put(fieldClass.getName(), fas);
			}
			flowStatus = inputFlow.getStatus();
			flowOrder = inputFlow.getOrder();
			return "flow";
		}
	}

	@Override
	public String list() throws Exception {
		page = flowFieldAttributeManager.getAllFlowFieldAttribute(page);
		return SUCCESS;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			entity = flowFieldAttributeManager.getFlowFieldAttribute(id);
		} else {
			entity = new FlowFieldAttribute();
		}
	}

	@Override
	public String save() throws Exception {
		flowFieldAttributeManager.saveFlowFieldAttribute(entity);
		return RELOAD;
	}

	public void setFlowFieldAttributeManager(
			FlowFieldAttributeManager flowFieldAttributeManager) {
		this.flowFieldAttributeManager = flowFieldAttributeManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Map<String, Map> getView() {
		return view;
	}

	public void setView(Map<String, Map> view) {
		this.view = view;
	}

	public Integer getFlowClassId() {
		return flowClassId;
	}

	public void setFlowClassId(Integer flowClassId) {
		this.flowClassId = flowClassId;
	}

	public Integer getFlowOrder() {
		return flowOrder;
	}

	public void setFlowOrder(Integer flowOrder) {
		this.flowOrder = flowOrder;
	}

	public Integer getFlowStatus() {
		return flowStatus;
	}

	public void setFlowStatus(Integer flowStatus) {
		this.flowStatus = flowStatus;
	}

	public Integer getFlowId() {
		return flowId;
	}

	public void setFlowId(Integer flowId) {
		this.flowId = flowId;
	}

	public void setFieldAttributeManager(
			FieldAttributeManager fieldAttributeManager) {
		this.fieldAttributeManager = fieldAttributeManager;
	}

	public void setFlowManager(FlowManager flowManager) {
		this.flowManager = flowManager;
	}

	public void setWorkOrderStepManager(
			WorkOrderStepManager workOrderStepManager) {
		this.workOrderStepManager = workOrderStepManager;
	}

	public void setWorkOrderManager(WorkOrderManager workOrderManager) {
		this.workOrderManager = workOrderManager;
	}

	public Integer getWorkOrderId() {
		return workOrderId;
	}

	public void setWorkOrderId(Integer workOrderId) {
		this.workOrderId = workOrderId;
	}

}
